package cz.jm.web.jmlib.client.dialog.validation.validators;

import cz.jm.web.jmlib.client.dialog.validation.HasValidationValue;

/** Validator kontroluje silu hesla. Vyzadovany je vyskyt maleho pismena, 
 * velkeho pismena a specialneho znaku pri urcenej minimalnej dlzke hesla.
 * */
public class PasswordValidator extends RegexpValidator {
	public static final String PASSWORD_PATTERN_SMALL_LETTER = ".*[a-z].*";
	public static final String PASSWORD_PATTERN_DIG_LETTER = ".*[A-Z].*";
	public static final String PASSWORD_PATTERN_DECIMAL = ".*\\d.*";
	private static final int MINIMAL_PASSWORD_LENGTH = 6;
	
	public PasswordValidator(HasValidationValue valueHolder) {
	    super("", valueHolder, constants.invalidPassword());
    }
	
	
	@Override
    public boolean validate() {
		boolean isValid = false;
		setPattern(PASSWORD_PATTERN_SMALL_LETTER);
		if (super.validate()) {
			setPattern(PASSWORD_PATTERN_DIG_LETTER);
			if (super.validate()) {
				setPattern(PASSWORD_PATTERN_DECIMAL);
				if (super.validate()) {
					if (getValueHolder().getValidationValue().length() >= MINIMAL_PASSWORD_LENGTH) {
						isValid = true;
					}
				}
			}
		}
		return isValid;
    }
}
